﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using Util;
namespace DAO
{
    /// <summary>
    /// Summary description for FriendRequest
    /// </summary>
    public class FriendRequest
    {
        DBConnection DBConnection;
        public FriendRequest()
        {
            DBConnection = new DBConnection();
            //
            // TODO: Add constructor logic here
            //
        }
        /// <summary>
        /// Get all DTO.FriendRequest instance from DB.
        /// </summary>
        /// <returns>List<> of all DTO.FriendRequest instances.</returns>
        public List<DTO.FriendRequest> GetAllFriendRequests()
        {
            Log.LogInfo("Begin GetAllFriendRequests()");
            String query = "SELECT * FROM [Friend_Request];";
            SqlDataReader reader = DBConnection.ExecuteReader(query);
            List<DTO.FriendRequest> friendRequestDTOs = new List<DTO.FriendRequest>();
            try
            {
                while (reader.Read())
                {
                    friendRequestDTOs.Add(new DTO.FriendRequest(reader));
                }
            }
            catch (Exception ex)
            {
                Log.LogError("Error GetAllFriendRequests(): " + ex.StackTrace);
            }

            Log.LogInfo("End GetAllFriendRequests()");
            return friendRequestDTOs;
        }
        /// <summary>
        /// Get DTO.FriendRequest instance from DB.
        /// </summary>
        /// <param name="ID">FriendRequest ID</param>
        /// <returns>DTO.FriendRequest instance which has the specific ID.</returns>
        public DTO.FriendRequest GetFriendRequest(String ID)
        {
            Log.LogInfo("Begin GetFriendRequest(String ID)");
            String query = "SELECT * FROM [Friend_Request] WHERE [Friend_Request].[ID]='" + ID + "';";
            SqlDataReader reader = DBConnection.ExecuteReader(query);
            DTO.FriendRequest friendRequestDTO = null;
            try
            {
                if (reader.Read())
                    friendRequestDTO = new DTO.FriendRequest(reader);
            }
            catch (Exception ex)
            {
                Log.LogError("Error GetFriendRequest(String ID): " + ex.Message);
            }

            Log.LogInfo("End GetFriendRequest(String ID)");
            return friendRequestDTO;
        }

        /// <summary>
        /// Insert FriendRequest into database.
        /// </summary>
        /// <param name="FriendRequest">DTO.FriendRequest instance to insert into database.</param>
        public int InsertFriendRequest(DTO.FriendRequest FriendRequest)
        {
            Log.LogInfo("Begin InsertFriendRequest()");
            String query = "";
            query += "INSERT INTO  FRIEND_REQUEST ([From],[To],Time) VALUES ('" + FriendRequest.From + "','" + FriendRequest.To + "','" + FriendRequest.Time + "')";
            query += "";
            int result = DBConnection.ExecuteNonQuery(query);
            DBConnection.Close();
            Log.LogInfo("Insert " + result.ToString() + "row(s).");
            Log.LogInfo("End InsertFriendRequest()");
            return result;

        }

        /// <summary>
        /// Update FriendRequest info into database.
        /// </summary>
        /// <param name="FriendRequest">FriendRequest instance to be updated.</param>
        public int UpdateFriendRequest(DTO.FriendRequest FriendRequest)
        {
            Log.LogInfo("Begin UpdateFriendRequest()");
            String query = "UPDATEFriendRequest SET [From] = '" + FriendRequest.From + "'," + " [To] = '" + FriendRequest.To + "'Time ='" + FriendRequest.Time + "' WHERE ID = '" + FriendRequest.ID + "'";
            int result = DBConnection.ExecuteNonQuery(query);
            DBConnection.Close();
            Log.LogInfo("Update " + result.ToString() + "row(s).");
            Log.LogInfo("End InsertFriendRequest()");
            return result;
        }
        /// <summary>
        /// Delete a specific FriendRequest from DB.
        /// </summary>
        /// <param name="FriendRequest">The FriendRequest to be deleted.</param>
        public int DeleteFriendRequest(DTO.FriendRequest FriendRequest)
        {
            Log.LogInfo("Begin DeleteFriendRequest()");
            String query = "DELETE FROM [Friend_Request] WHERE [Friend_Request].[ID]='" + FriendRequest.ID + "';";
            int result = DBConnection.ExecuteNonQuery(query);
            DBConnection.Close();
            Log.LogInfo("Deleted " + result.ToString() + "row(s).");
            Log.LogInfo("End DeleteFriendRequest()");
            return result;
        }

        /// <summary>
        /// Delete a FriendRequest in database using ID.
        /// </summary>
        /// <param name="ID">FriendRequest ID.</param>
        public int DeleteFriendRequest(String ID)
        {
            Log.LogInfo("Begin DeleteFriendRequest()");
            String query = "DELETE FROM [Friend_Request] WHERE [Friend_Request].[ID]='" + ID + "';";
            int result = DBConnection.ExecuteNonQuery(query);
            DBConnection.Close();
            Log.LogInfo("Deleted " + result.ToString() + "row(s).");
            Log.LogInfo("End DeleteFriendRequest()");
            return result;
        }
    }
}